#coding:utf8
from config import *
from config_mouse_start import *
from routin_simple import *
def alt_ini_imp(st):
    cfg.alt_mode_other = False
    if 'alt_l' in st:
        cfg.alt_mode = 1
    else:
        cfg.alt_mode = 2
    return

def chg_alt_mode():
    if cfg.alt_mode==1:
            cfg.alt_mode = 2
            return
    cfg.alt_mode=1

def deal_ctrl_move_5_2(v,isx):
    x, y = get_cur_pos()    
    idx = get_screen_indx(x, y)
    aa,b,c,dd =  screen_config_for_indx[idx]
    a,ff = change_sum(v)
    xsca,ysca = get_win_sca(idx)
    dsca = xsca
    if not isx:
        dsca = ysca
    
    dl = c-aa
    if not isx:
        dl = dd-b


    x, y = getp(None,False)        
    sx, sy = cfg.fix_pos
    dx = x - sx 
    dy = y - sy         
    if dx==0 and dy==0:
        dx,dy = cfg.last_m
    du = math.atan2(dy,dx)
    if a:
        ddx = ff*dsca
    elif v[-1]=='.':
        ddx = int(float(v))
    else:
        ddx = ff*dl/100

    x = round(math.cos(du)*ddx)
    y = round(math.sin(du)*ddx)    
    set_cfg_sum_0()
    do_step_move(x,y,True)    

def deal_ctrl_move_5(v,isx):
    x, y = get_cur_pos()    
    idx = get_screen_indx(x, y)
    aa,b,c,dd =  screen_config_for_indx[idx]
    a,ff = change_sum(v)
    xsca,ysca = get_win_sca(idx)
    dsca = xsca
    if not isx:
        dsca = ysca
    print('ct5',isx,dsca,a,ff)
    x, y = getp(None,False)        
    sx, sy = cfg.fix_pos
    dx = x - sx 
    dy = y - sy      
    if dx==0 and dy==0:
        dx,dy = cfg.last_m

    if isx:
        if dx==0:
            return
        if a:
            ddx = ff*dsca*dx/abs(dx)
        elif v[-1]=='.':
            ddx = int(float(v))*dx/abs(dx)
        else:
            ddx = ff*(c-aa)/100*dx/abs(dx)
        ddy = ddx*dy/dx
    else:
        if dy==0:
            return
        if a:
            ddy = ff*dsca*dy/abs(dy)
        elif v[-1]=='.':
            ddy = int(float(v))*dy/abs(dy)
        else:
            ddy = ff*(dd-b)/100*dy/abs(dy)
        ddx = ddy*dx/dy
    x = round(ddx)
    y = round(ddy)
    set_cfg_sum_0()
    do_step_move(x,y,True)

def is5f(st):
    if 'ctrl_l' in st:
        if cfg.sum[0]=='5':
            if cfg.sum[1]!='0':
                v = cfg.sum[1:]
                return True,True,v
            else:
                v = cfg.sum[2:]
                return True,False,v
        return False,False,''
    if cfg.sum[-1]=='5':
        v = cfg.sum[0:-1]
        return True,True,v
    if cfg.sum[-2:]=='50':
        v = cfg.sum[0:-2]
        return True,False,v
    return False,False,''
def is5f_2(st):
    if cfg.sum[:2]=='50':
        return True,False,cfg.sum[2:]
    if cfg.sum[0]=='5':
        return True,True,cfg.sum[1:]
    return False,False,0

def deal_ctrl_move(st):
    if len(cfg.sum)==1 and cfg.sum in '12346789':
        cfg.sum += '1'
    if len(cfg.sum)<2:
        return
    if 'ctrl_l' not in st:
        is5,isx,v =  is5f(st)
        if is5:
            if not v or v=='.':
                return
            deal_ctrl_move_5(v,isx)
            return 1
    else:
        is5,isx,v =  is5f_2(st)
        if is5:
            if not v or v=='.':
                return
            deal_ctrl_move_5_2(v,isx)
            return 1
    
    si = cfg.sum[0]
    v =  cfg.sum[1:]

    if 'ctrl_l' not in st:
        si = cfg.sum[-1]
        v = cfg.sum[:-1]
    if v=='.' :
        return
    
    if si not in '12346789':
        return    
    
    x, y = get_cur_pos()    
    idx = get_screen_indx(x, y)
    aa,b,c,dd =  screen_config_for_indx[idx]
    dx,dy = arrm[int(si)]
    a,ff = change_sum(v)
    xsca,ysca = get_win_sca(idx)
    dsca = xsca
    
    if si in '28':
        dsca = ysca    
    
    if a:    
        d = round(ff*cfg.jump_len)   
    elif v[-1]=='.':
        d = round(float(v)*dsca)       
    else:        
        if si in '46':
            d = ff*(c-aa)/100
        elif si in '28':
            d = ff*(dd-b)/100
        else:
            d = min(c,dd)*ff/100
    x = round(dx*d)
    y = round(dy*d)
    set_cfg_sum_0()     
    do_step_move(x,y,True)   

def alt4_dx(isx,aff=None):           
    print('alt4_dx',isx,aff) 
    x2, y2 = getp(None,False)        
    sx, sy = cfg.fix_pos
    dx = x2 - sx 
    dy = y2 - sy          

    x, y = get_cur_pos()
    idx = get_screen_indx(x, y)
    aa,b,c,d =  screen_config_for_indx[idx]    
    if aff==None:
        a,ff = change_sum(cfg.sum)
    else:
        a,ff = aff
    xsca,ysca = get_win_sca(idx)
    dsca = xsca
    if not isx:
        dsca = ysca    

    ff = round(ff*dsca)
    if aff==None:
        if cfg.sum[-2:]=='.0':
            a = True
            ff = round(float(cfg.sum))
        if cfg.sum[-3:]=='.00':
            a = False
            ff = round(float(cfg.sum))

    if isx:
        if a:
            dstx = aa+ff
        else:
            dstx = c-ff
        if (dstx-sx)*dx<=0:
            return False,0,0
            
        dsty = dy*(dstx-sx)/dx+y2
        return True,dstx,dsty

    if a:
        dsty = b+ff
    else:
        dsty = d-ff
    if (dsty-sy)*dy<=0:
        return False,0,0
        
    dstx = dx*(dsty-sy)/dy+x2    
    return True,dstx,dsty

def alt_fire4():        
    if  cfg.sum=='0':        
        return     
    a ,dstx,dsty = alt4_dx(cfg.alt_mode==1)
    if not a:
        alt_fire3()
        return
    
    set_cfg_sum_0()
    con_set_pos(dstx,dsty)
    clear(True)
    cfg.set_fix_time = time.time()
    return    

def alt_fire3():        
    if cfg.sum=='0':
        return
    
    x, y = get_cur_pos()
    idx = get_screen_indx(x, y)
    aa,b,c,d =  screen_config_for_indx[idx]    
    a,ff = change_sum(cfg.sum)
    xsca,ysca = get_win_sca(idx)
    print('sca',xsca,ysca)
    dsca = xsca
    if cfg.alt_mode!=1:
        dsca = ysca    

    ff = round(ff*dsca)

    if cfg.sum[-2:]=='.0':
        a = True
        ff = round(float(cfg.sum))
    if cfg.sum[-3:]=='.00':
        a = False
        ff = round(float(cfg.sum))
    if a:
        if  cfg.alt_mode==1:
            dx = aa+ff
            dy = y
        else:
            dx = x
            dy = b+ff
    else:
        if  cfg.alt_mode==1:
            dx = c-ff
            dy = y
        else:
            dx = x
            dy = d-ff
    set_cfg_sum_0()
    print('pos',dx,dy)
    con_set_pos(dx,dy)
    clear(True)
    cfg.show_one_on = True
    cfg.set_fix_time = time.time()
    return

def alt_fire2():    
    x, y = get_cur_pos()
    idx = get_screen_indx(x, y)
    aa,b,c,d =  screen_config_for_indx[idx]    
    if '.' not in cfg.sum:
        if  cfg.alt_mode==1:
            dx = int(float(cfg.sum))
            dy = y
        else:
            dx = x
            dy = int(float(cfg.sum))
    elif cfg.sum[-1]=='.':
        if  cfg.alt_mode==1:
            dx = aa+c-int(float(cfg.sum))
            dy = y
        else:
            dx = x
            dy = b+d-int(float(cfg.sum))
    elif not is_ff(cfg.sum): 
        if  cfg.alt_mode==1:
            dx = aa+round(float(cfg.sum)/100*c)
            dy = y
        else:
            dx = x
            dy = b+round(float(cfg.sum)/100*d)
    else:
        if  cfg.alt_mode==1:
            dx = aa+c-round(float(cfg.sum)/100*c)
            dy = y
        else:
            dx = x
            dy = b+d-round(float(cfg.sum)/100*d)
    set_cfg_sum_0()
    con_set_pos(dx,dy)
    clear(True)
    cfg.set_fix_time = time.time()
    return
   
def alt_fire():
    if  cfg.sum=='0':        
        return           
    
    if cfg.sum=='0.':
        cfg.sum='5.00'
    if cfg.sum=='0.00':
        pause_xing()
        set_cfg_sum_0()
        return
    if cfg.sum=='0.0':
        cfg.sum = '-0'

    if '-' not in cfg.sum:
        if not cfg.alt_mode_other:
            alt_fire4()
        else:
            alt_fire2()
        return
    
    if '.' in cfg.sum or cfg.sum=='-0':
        cfg.sum = cfg.sum.replace('-','')
    x, y = get_cur_pos()
    idx = get_screen_indx(x, y)
    aa,b,c,d =  screen_config_for_indx[idx]        
    dx = x-aa
    dy = y-b

    idx = get_screen_indx(x, y)
    if cfg.sum[-1]=='.' or cfg.sum=='0':
        idx += 1
        if idx >= len(screen_config_for_indx):
            idx = 0    
    aa,b,c,d =  screen_config_for_indx[idx]
    f = int(float(cfg.sum))
    if cfg.sum=='0':
        x = dx+aa
        if not alt_fix_y:
            y = dy+b

    elif cfg.alt_mode==1:
        x = f+aa
        if not alt_fix_y:
            y = dy+b
    else:
        y = f+b
        x = dx+aa

    set_cfg_sum_0()
    con_set_pos(x,y)
    clear(True)
    cfg.set_fix_time = time.time()

def deal_alt_mode(a,st):
    if a==-1:
        return
    cfg.alt_mode_other = True
    if a>=0 and a<=10:
        if a==10:
            a='.'
        if len(cfg.sum)>sum_max_l:
            return
        pp = ''
        if a=='.':
            pp ='0'
        if cfg.sum=='0' :
            cfg.sum = ''+pp
        if cfg.sum=='-0':
            cfg.sum = '-'+pp
        cfg.sum += str(a)
        return
    
    if a==12:
        if cfg.sum[0]=='-':
            cfg.sum = cfg.sum[1:]
        else:
            cfg.sum = '-'+cfg.sum
        return
     
    if a==14:
        set_cfg_sum_0()
        return
    
    if a==11:
        chg_alt_mode()
        return
    
    if a==15:
        alt_fire()
def ctrl_ini_imp(st):        
    if cfg.x2_mode:
        return
    
    if not cfg.lock:
        clear_lock()        
        ori = cfg.sum
        orif = cfg.force_show
        if cfg.sum=='0.':
            if 'ctrl_l' in st:
                set_to_last(True)
            else:
                set_to_last(False)
            set_cfg_sum_0()
            return
        ret = deal_ctrl_move(st)
        if ori !='0' and cfg.sum=='0' and not cfg.w2showing and not orif:
            deal_e(False,0,False)

    if cfg.lock:
        cfg.lock_xing_mode = 1
        if ctrl_into_jia and 'ctrl_l' in st:
            cfg.lock_xing_mode = 2
        